package com.sun.mongodb.core.util;

import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页工具类
 *
 * @author zyl
 */
@Data
public class PageUtils<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 是否查询总数量
	 */
	private Boolean queryCount = true;
	/**
	 * 总记录数
	 */
	private long total;
	/**
	 * 每页记录数
	 */
	private int size = 10 ;
	/**
	 * 总页数
	 */
	private long pages;
	/**
	 * 当前页数
	 */
	private int page = 1;
	/**
	 * 列表数据
	 */
	private List<T> list = Collections.emptyList();

	public PageUtils(){

	}

	public PageUtils(int currPage, int pageSize){
		this.size = pageSize;
		this.page = currPage;
	}
	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param currPage    当前页数
	 */
	public PageUtils(int currPage, int pageSize,List<T> list, int totalCount) {
		this.list = list;
		this.total = totalCount;
		this.size = pageSize;
		this.page = currPage;
		this.pages = (int)Math.ceil((double)totalCount/pageSize);
	}

	public long getPages() {
		if(this.getSize() == 0L) {
			return 0L;
		} else {
			long pages = this.getTotal() / this.getSize();
			if(this.getTotal() % this.getSize() != 0L) {
				++pages;
			}
			return pages;
		}
	}
	
}
